<?php

class m_user extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    //get all user
    function getAll() {
        $strSQL = "SELECT * FROM tb_admin";
        $query = $this->db->query($strSQL);
        if ($query->num_rows() > 0) {
            foreach ($query->result_array() as $row) {
                $result[] = $row;
            }
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }
    //-- get user by id
    function getById($pStrUserId) {
        $strSQL = "SELECT * FROM tb_admin WHERE id_admin = ?";
        $strQuery = $this->db->query($strSQL, $pStrUserId);
        if ($strQuery->num_rows() > 0) {
            $arrResult = $strQuery->row_array();
            $strQuery->free_result();
            return $arrResult;
        } else {
            return array();
        }
    }
    //-- get user by id
    function getByUsername($pStrUserId) {
        $strSQL = "SELECT * FROM tb_admin WHERE username = ?";
        $strQuery = $this->db->query($strSQL, $pStrUserId);
        if ($strQuery->num_rows() > 0) {
            $arrResult = $strQuery->row_array();
            $strQuery->free_result();
            return $arrResult;
        } else {
            return array();
        }
    }
    //count code
    function count() {
        return $this->db->count_all_results("tb_admin");
    }

    //get all fileds
    function getAllCode($pArrParams) {
        $strSQL = "SELECT * FROM tb_admin LIMIT ?,?";
        $strQuery = $this->db->query($strSQL, $pArrParams);
        if ($strQuery->num_rows() > 0) {
            foreach ($strQuery->result_array() as $strRow) {
                $arrResult[] = $strRow;
            }
            $strQuery->free_result();
            return $arrResult;
        } else {
            return array();
        }
    }

    //-- save user
    function saveAdd($pArrSaveParams) {
        $strSQL = "INSERT INTO tb_admin (id_admin, user_nm, password, login_st, lock_st, rule_tp, modi_id, modi_datetime)
                VALUES (?, ?, ?, ?, ?, ?, ?, NOW())";
        return $this->db->query($strSQL, $pArrSaveParams);
    }

    //-- update user
    function saveEdit($pArrEditParams) {
        $strSQL = "UPDATE tb_admin SET user_nm = ?, password = ?, 
                   login_st = ?, lock_st = ?, rule_tp = ?, modi_id = ?, modi_datetime=NOW() WHERE id_admin = ?";
        return $this->db->query($strSQL, $pArrEditParams);
    }

    //-- update passowrd user
    function saveUpdatePwd($pArrEditParams) {
        $strSQL = "UPDATE tb_admin SET password = ?, modi_id = ?, modi_datetime = NOW() WHERE id_admin = ?";
        return $this->db->query($strSQL, $params);
    }
    //delete user
    function delete($pStrUserId) {
        $strSQL = "DELETE FROM tb_admin WHERE id_admin = ?";
        return $this->db->query($strSQL, $pStrUserId);
    }

}
?>